package com.itheima.reggie.controller;


import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    HttpSession session;
    //用户登录页面
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Map<String,String> param){
        String username = param.get("username");
        String password = param.get("password");
        ResultInfo resultInfo = employeeService.login(username, password);
        if (resultInfo.getCode()==1) {
            Employee employee = (Employee) resultInfo.getData();
            session.setAttribute(Constant.SESSION_EMPLOYEE,employee);
        }
        return resultInfo;
    }

    //用户注销
    @PostMapping("/employee/logout")
    public ResultInfo logout(){
        session.invalidate();
        return ResultInfo.success(null);
    }

    //员工列表
    @GetMapping("/employee/find")
    public ResultInfo findList(String name){

        List<Employee> list=employeeService.findList(name);
        return ResultInfo.success(list);
    }

    //添加员工
    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee){
        employeeService.save(employee);
        return ResultInfo.success(null);
    }
    //编辑员工回显
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Employee employee=employeeService.findById(id);
        return ResultInfo.success(employee);

    }
    //编辑员工
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee){
        employeeService.update(employee);
        return ResultInfo.success(null);
    }

}
